package com.jianganming.clone;

import java.io.*;

/**
 * 深克隆
 */
public class Clazz2 implements Cloneable,Serializable {
    private String className;
    private Student student;

    @Override
    protected Clazz2 clone() throws CloneNotSupportedException {
        return (Clazz2)super.clone();
    }

    /**
     * 深克隆
     * @return
     * @throws Exception
     */
    protected Clazz2 deepClone()throws Exception{
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(bos);
        oos.writeObject(this);
        ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
        ObjectInputStream ois = new ObjectInputStream(bis);
        return (Clazz2)ois.readObject();
    }


    public Clazz2(String className, Student student) {
        this.className = className;
        this.student = student;
    }

    public String getClassName() {
        return className;
    }

    public Student getStudent() {
        return student;
    }
}



class CitationTest2{
    public static void main(String[] args)throws Exception {
        Student student1 = new Student();
        student1.setAge(12);
        student1.setAnme("张三");
        System.out.println(student1);
        Clazz2 clazz = new Clazz2("高一01",student1);
        Clazz2 clone = clazz.deepClone();
        Student student = clone.getStudent();
        student.setAnme("李四");
        System.out.println(student);//这里就是克隆对象
        System.out.println(student1);//这里就是原对象
    }
}
